﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace DMS.Business.WebControls
{
    [ToolboxData("<{0}:TreeNodePlus runat=server></{0}:TreeNodePlus>")]
    public class TreeNodePlus : TreeNode
    {
        //private StateBag cm = new StateBag();
        private string _ContextMenu = string.Empty;
        private TreeNodeTypes _ShowContextMenu = TreeNodeTypes.None;
        public string ContextMenu
        {
            get
            {
                //if (cm["ContextMenu"] != null)
                //{
                //    return cm["ContextMenu"].ToString();
                //}

                return this._ContextMenu;
            }
            set
            {
                //cm.Add("ContextMenu", value);
                this._ContextMenu = value;
            }
        }
        public TreeNodeTypes ShowContextMenu
        {
            get
            {
                return this._ShowContextMenu;
            }
            set
            {
                this._ShowContextMenu = value;
            }
        }
        private bool IsHasContextMenu
        {
            get
            {
                if (string.IsNullOrEmpty(this._ContextMenu))
                {
                    return false;
                }
                if (this._ShowContextMenu == TreeNodeTypes.None)
                {
                    return false;
                }
                else if (this._ShowContextMenu == TreeNodeTypes.Root
                        && this.Parent != null)
                {
                    return false;
                }
                else if (this._ShowContextMenu == TreeNodeTypes.Leaf)
                {
                    return false;
                }
                else if (this._ShowContextMenu == TreeNodeTypes.Parent)
                {
                    return false;
                }

                return true;
            }
        }

        internal TreeNodePlus(TreeViewPlus tree)
        {
            this.ContextMenu = tree.ContextMenu;
            this.ShowContextMenu = tree.ShowContextMenu;
        }

        protected override void RenderPreText(HtmlTextWriter writer)
        {
            if (this.IsHasContextMenu)
            {
                string menuContent = this._ContextMenu;

                if (this._ContextMenu.Contains("%Text%"))
                {
                    menuContent = menuContent.Replace("%Text%", this.Text.Replace("'", "\""));
                }
                if (this._ContextMenu.Contains("%Value%"))
                {
                    menuContent = menuContent.Replace("%Value%", this.Value);
                }
                if (this._ContextMenu.Contains("%ValuePath%"))
                {
                    menuContent = menuContent.Replace("%ValuePath%", this.ValuePath);
                }
                if (this._ContextMenu.Contains("%ToolTip%"))
                {
                    menuContent = menuContent.Replace("%ToolTip%", this.ToolTip.Replace("'", "\""));
                }
                if (this._ContextMenu.Contains("%NavigateUrl%"))
                {
                    menuContent = menuContent.Replace("%NavigateUrl%", this.NavigateUrl);
                }

                writer.AddAttribute("id", string.Format("treeNodePlus_{0}", this.Value));
                writer.AddAttribute("oncontextmenu", menuContent);
                writer.RenderBeginTag(HtmlTextWriterTag.Span);
            }

            base.RenderPreText(writer);
        }
        protected override void RenderPostText(HtmlTextWriter writer)
        {
            base.RenderPostText(writer);

            if (IsHasContextMenu)
            {
                writer.RenderEndTag();
            }
        }

        protected override void LoadViewState(object state)
        {
            base.LoadViewState(state);
        }
        protected override object SaveViewState()
        {
            return base.SaveViewState();
        }
    }
}